package com.example.smartcity.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.ProductBean

/**
 * @Author: Tailyou
 * @CreateDate: 2021/10/7 19:11
 * @Email: tailyou@163.com
 * @Description: RecycleView数据适配器（店铺列表）
 */
class ProductListHAdapter(
    private var data: List<ProductBean>,
    var listener: OnItemClickListener
) :
    RecyclerView.Adapter<ProductListHAdapter.MViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MViewHolder {
        var view =
            LayoutInflater.from(parent.context).inflate(R.layout.layout_item_list_h_product, null)
        return MViewHolder(view)
    }

    override fun onBindViewHolder(holder: MViewHolder, position: Int) {
        holder.name.text = data[position].name
        holder.price.text = "¥ ${data[position].price}"
        App.fetchRCImage(data[position].imgUrl, holder.pic, 32, R.mipmap.ic_more_service)
        holder.itemView.setOnClickListener {
            listener.onClick(position)
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    class MViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var pic: ImageView = itemView.findViewById(R.id.iv_pic)
        var name: TextView = itemView.findViewById(R.id.tv_name)
        var price: TextView = itemView.findViewById(R.id.tv_price)
    }

    interface OnItemClickListener {
        fun onClick(position: Int)
    }

}